package com.t18;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

import static net.mindview.util.Print.print;

/**
 * Created by xuyiwei on 2018/9/5.
 */
class House implements Serializable{
}
class Animal implements Serializable{
    private static String name;
    private House preferedHouse;
    Animal(String nm,House h){ name = nm; preferedHouse = h;  }
    @Override
    public String toString() {
        return "Animal{"+name+" " + "name='" + super.toString() + '\'' + ", preferedHouse=" + preferedHouse + '}';
    }
}
public class MyWorld {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        House house = new House();
        List<Animal> animals = new ArrayList<>();
        animals.add(new Animal("Bosco the dog",house));
        animals.add(new Animal("Ralph the hamster",house));
        print("animals: "+animals);
        new Animal(" the",house);
        ByteArrayOutputStream buf1 = new ByteArrayOutputStream();
        ObjectOutputStream o1 = new ObjectOutputStream(buf1);
        o1.writeObject(animals); o1.writeObject(animals);
        ByteArrayOutputStream buf2 = new ByteArrayOutputStream();
        ObjectOutputStream o2 = new ObjectOutputStream(buf2);
        o2.writeObject(animals);
        ObjectInputStream in1 = new ObjectInputStream(new ByteArrayInputStream(buf1.toByteArray()));
        ObjectInputStream in2 = new ObjectInputStream(new ByteArrayInputStream(buf2.toByteArray()));
        List animals1 = (List)in1.readObject(),animals2= (List)in1.readObject(),animals3= (List)in2.readObject();
        print(animals1);
        print(animals2);
        print(animals3);
        /**
         * animals: [Animal{name='com.t18.Animal@49476842', preferedHouse=com.t18.House@78308db1}, Animal{name='com.t18.Animal@27c170f0', preferedHouse=com.t18.House@78308db1}]
         [Animal{name='com.t18.Animal@5caf905d', preferedHouse=com.t18.House@27716f4}, Animal{name='com.t18.Animal@8efb846', preferedHouse=com.t18.House@27716f4}]
         [Animal{name='com.t18.Animal@5caf905d', preferedHouse=com.t18.House@27716f4}, Animal{name='com.t18.Animal@8efb846', preferedHouse=com.t18.House@27716f4}]
         [Animal{name='com.t18.Animal@2a84aee7', preferedHouse=com.t18.House@a09ee92}, Animal{name='com.t18.Animal@30f39991', preferedHouse=com.t18.House@a09ee92}]
         */
    }


}
